package com.hp.utils;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.*;

public class JDBCUtils {
    private static DataSource dataSource=new ComboPooledDataSource();
    //获得数据源
    public static DataSource getDataSource(){
        return dataSource;
    }
    //获取数据库连接
    public static Connection getConnection() {
        try {
           return dataSource.getConnection();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    //释放资源的方法
    public static void close(Connection con, Statement stat, ResultSet rs) {
        if(con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(stat != null) {
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    //重载释放资源的方法
    public static void close(Connection con, Statement stat) {
        close(con,stat,null);
    }
}